package cc.lj.internal.payment.domain.settlement.service.impl;

import cc.lj.internal.payment.domain.pay.repository.IProductConumeRepository;
import cc.lj.internal.payment.domain.settlement.repository.ICouponsSubsidyDetailRepository;
import cc.lj.internal.payment.domain.settlement.repository.ICouponsSubsidyRepository;
import cc.lj.internal.payment.domain.settlement.service.ICouponsSubsidyDomainService;
import cc.lj.internal.payment.domain.settlement.vo.CouponsSubsidyDO;
import cc.lj.internal.payment.domain.settlement.vo.CouponsSubsidyDetailDO;
import cc.lj.internal.payment.domain.settlement.vo.convert.CouponsSubsidyDetailPOConvertDO;
import cc.lj.internal.payment.domain.settlement.vo.convert.CouponsSubsidyPOConvertDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 电子券补贴 domain service 实现类
 * @author liangxifeng
 * @date 2022/10/23 17:13
 */
@Service("couponsSubsidyDomainServiceImpl")
@Slf4j
public class CouponsSubsidyDomainServiceImpl implements ICouponsSubsidyDomainService {
    @Resource(name = "couponsSubsidyRepository")
    private ICouponsSubsidyRepository repository;

    @Resource(name = "couponsSubsidyDetailRepository")
    private ICouponsSubsidyDetailRepository detailRepository;

    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
    public Integer addSubsidyAndDetail(CouponsSubsidyDO subsidyDO, List<CouponsSubsidyDetailDO> detailDOList) {
        //新增券分摊表数据后，获得新增主键
        Integer subsidyId = repository.insert(CouponsSubsidyPOConvertDO.INSTANCE.convert(subsidyDO));
        //循环新增补贴明细表数据
        detailDOList.forEach(detailDO -> detailRepository.insert(CouponsSubsidyDetailPOConvertDO.INSTANCE.convert(detailDO)));
        return subsidyId;
    }

    @Override
    public Integer insert(CouponsSubsidyDO subsidyDO) {
        return repository.insert(CouponsSubsidyPOConvertDO.INSTANCE.convert(subsidyDO));
    }

    @Override
    public CouponsSubsidyDO getById(Integer id) {
        return CouponsSubsidyPOConvertDO.INSTANCE.convert( repository.getById(id) );
    }


    @Override
    public List<CouponsSubsidyDO> getList(CouponsSubsidyDO subsidyDO) {
        return repository.getList(CouponsSubsidyPOConvertDO.INSTANCE.convert(subsidyDO))
                .stream()
                .map( x-> CouponsSubsidyPOConvertDO.INSTANCE.convert(x))
                .collect(Collectors.toList());
    }
}
